﻿#ifndef CUSTOM_SSSSSCATTER_PASS_INCLUDED
#define CUSTOM_SSSSSCATTER_PASS_INCLUDED

struct Attributes {
	float3 positionOS : POSITION;
	float2 baseUV : TEXCOORD0;
};

struct Varyings {
	float4 positionCS_SS : SV_POSITION;
	float2 baseUV : VAR_BASE_UV;
};

//sampler2D _MainTex;
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);


//sampler2D _MainTex;

//sampler2D _MaskTex;
half4 _BlurVec;
float _BlurStr;
float _SoftDepthBias;

//sampler2D _BlurTex;
TEXTURE2D(_BlurTex);
//sampler2D _CameraDepthTexture;
//sampler2D _MaskTex;
TEXTURE2D(_MaskTex);
SAMPLER(sampler_MaskTex);
//TEXTURE2D(_CameraDepthTexture);
//sampler2D_float _CameraDepthTexture;
half _EffectStr;
half _PreserveOriginal;
half4 _GloabScatteringColor;

inline float SAMPLE_INVERSE_DEPTH(float2 uvs)
{
	//float t = 1;
	////unity_CameraProjection._m11;
	//float4 depth = SAMPLE_TEXTURE2D(_CameraDepthTexture, sampler_MainTex, uvs);
	//float z = LinearEyeDepth(depth.r);
	///*
	//			Voodoo math on visible size multiplier here that i dont understand, but it works
	//			*/
	//float size = 0.5 / (z + 0.5);
	//return size * t * 0.6;
	return 0.2;
	
}

inline float SAMPLE_INVERSE_DEPTH_LINEAR(float2 uvs)
{
	//float4 depth = SAMPLE_TEXTURE2D(_CameraDepthTexture, sampler_MainTex, uvs);
	//return saturate(1.0 - Linear01Depth(tex2D(_CameraDepthTexture, uvs).r));
	return 0.2;

}

//float2 TransformBaseUV(float2 baseUV)
//{
//	float4 baseST = INPUT_PROP(_BaseMap_ST);
//	return baseUV * baseST.xy + baseST.zw;
//}

Varyings SSSSScatterPassVertex (Attributes input) {
	Varyings output;	
	float3 positionWS = TransformObjectToWorld(input.positionOS);
	output.positionCS_SS = TransformWorldToHClip(positionWS);
	//output.baseUV = TransformBaseUV(input.baseUV);
	output.baseUV = input.baseUV;
	if (_ProjectionParams.x < 0.0)
	{
		//output.baseUV.y = 1.0 - output.baseUV.y;
	}
	return output;
}

Varyings SSSSScatterCombinePassVertex(Attributes input)
{
	Varyings output;
	float3 positionWS = TransformObjectToWorld(input.positionOS);
	output.positionCS_SS = TransformWorldToHClip(positionWS);
	//output.baseUV = TransformBaseUV(input.baseUV);
	output.baseUV = input.baseUV;
	if (_ProjectionParams.x < 0.0)
	{
		//output.baseUV.y = 1.0 - output.baseUV.y;
	}
	return output;
}


float4 SSSSScatterPassFragmentII(Varyings input) : SV_TARGET
{
					//If theres nothing in the mask texture, then we dont need to blur/process it
	//float4 testMask = tex2D(_MaskTex, i.uv);
	float4 testMask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, input.baseUV);
	if (testMask.a < 0.003 || testMask.r + testMask.g + testMask.b < 0.005)
		discard;

	//float4 col = tex2D(_MainTex, i.uv);
	float4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.baseUV);
	half2 blurvec = _BlurVec.xy;
	float d = SAMPLE_INVERSE_DEPTH(input.baseUV);
	float str = _BlurStr * d * testMask.a;

	float dlin = SAMPLE_INVERSE_DEPTH_LINEAR(input.baseUV);
	if (dlin < 0.1)
		discard;
				

				//Gaussian blur
				/*
				col *= 0.38;
				col += tex2D(_MainTex, i.uv + blurvec * 1 * str)*0.18;
				col += tex2D(_MainTex, i.uv - blurvec * 1 * str)*0.18;
				col += tex2D(_MainTex, i.uv + blurvec * 2 * str)*0.09;
				col += tex2D(_MainTex, i.uv - blurvec * 2 * str)*0.09;
				col += tex2D(_MainTex, i.uv + blurvec * 3 * str)*0.04;
				col += tex2D(_MainTex, i.uv - blurvec * 3 * str)*0.04;
				*/

				//Blur with depth check
				/**/
	float sum = 1;
	float diff = 0;
	float cont = 0;

				//HACK:
				//We multiply the depth sample vector by a 1.06 to get rid of the 1 pixel wide lines, 
				//that show up on the edges where the depth difference between samples is significant, 
				//i have no clue whats causing it and how to fix it properly :(
	for (uint n = 1; n <= 2; n++)
	{
		float contrib_base = 0.99 / (n + 2);

		//float4 colr = tex2D(_MainTex, i.uv + blurvec * n * str);
		float4 colr = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.baseUV);
		
		float dr = SAMPLE_INVERSE_DEPTH_LINEAR(input.baseUV + blurvec * n * str * 1.06);
		diff = abs(dlin - dr);
		cont = 1.0 - saturate(diff / _SoftDepthBias);
		cont *= contrib_base;
		col.rgb += colr.rgb * cont;
		sum += cont;

		//float4 coll = tex2D(_MainTex, i.uv - blurvec * n * str);
		float4 coll = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.baseUV);
		
		float dl = SAMPLE_INVERSE_DEPTH_LINEAR(input.baseUV - blurvec * n * str * 1.06);
		diff = abs(dlin - dl);
		cont = 1.0 - saturate(diff / _SoftDepthBias);
		cont *= contrib_base;
		col.rgb += coll.rgb * cont;
		sum += cont;
	}
	col.rgb /= sum;
				//return testMask;
	return col;
				//return pow(sum*0.5, 6);
	
	//return float4(1.0, 1.0, 1.0, 1.0);
}

float4 SSSSScatterPassFragment(Varyings input) : SV_TARGET
{
					//If theres nothing in the mask texture, then we dont need to blur/process it
	//float4 testMask = tex2D(_MaskTex, i.uv);
	float4 testMask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, input.baseUV);
	if (testMask.a < 0.003 || testMask.r + testMask.g + testMask.b < 0.005)
		discard;

	//float4 col = tex2D(_MainTex, i.uv);
	float4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.baseUV);
	half2 blurvec = _BlurVec.xy;
	float d = SAMPLE_INVERSE_DEPTH(input.baseUV);
	float str = _BlurStr * d * testMask.a;

	float dlin = SAMPLE_INVERSE_DEPTH_LINEAR(input.baseUV);
	if (dlin < 0.1)
		discard;
				
	float sum = 1;
	float diff = 0;
	float cont = 0;

				//HACK:
				//We multiply the depth sample vector by a 1.06 to get rid of the 1 pixel wide lines, 
				//that show up on the edges where the depth difference between samples is significant, 
				//i have no clue whats causing it and how to fix it properly :(
	for (uint n = 1; n <= 2; n++)
	{
		float contrib_base = 0.99 / (n + 2);

		//float4 colr = tex2D(_MainTex, i.uv + blurvec * n * str);
		float4 colr = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.baseUV + blurvec * n * str);
		
		float dr = SAMPLE_INVERSE_DEPTH_LINEAR(input.baseUV + blurvec * n * str * 1.06);
		diff = abs(dlin - dr);
		cont = 1.0 - saturate(diff / _SoftDepthBias);
		cont *= contrib_base;
		col.rgb += colr.rgb * cont;
		sum += cont;

		//float4 coll = tex2D(_MainTex, i.uv - blurvec * n * str);
		float4 coll = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.baseUV - blurvec * n * str);
		
		float dl = SAMPLE_INVERSE_DEPTH_LINEAR(input.baseUV - blurvec * n * str * 1.06);
		diff = abs(dlin - dl);
		cont = 1.0 - saturate(diff / _SoftDepthBias);
		cont *= contrib_base;
		col.rgb += coll.rgb * cont;
		sum += cont;
	}
	col.rgb /= sum;
				//return testMask;
	return col;
				//return pow(sum*0.5, 6);
	
	//return float4(1.0, 1.0, 1.0, 1.0);
}


float4 SSSSSCombinePassFragment(Varyings input) : SV_TARGET
{
	//float4 src = tex2D(_MainTex, input.baseUV);
	float4 src = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.baseUV);
	//half4 mask = tex2D(_MaskTex, input.baseUV);
	float4 mask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MainTex, input.baseUV);
	float fac = 1 - pow(saturate(max(max(src.r, src.g), src.b) * 1), 0.5);
	
	//float4 blr = tex2D(_BlurTex, input.baseUV);
	float4 blr = SAMPLE_TEXTURE2D(_BlurTex, sampler_MainTex, input.baseUV);
				//return blr;
				//return mask;
				//return src*0.5 + mask*0.5;
				//return src;
	blr = clamp(blr - src * _PreserveOriginal, 0, 50);
	blr.rgb *= mask.rgb;
	return src + blr * fac * _EffectStr * mask.a * _GloabScatteringColor;
	
	//return src;
}

#endif